<?php

namespace app\site\controller\site;


use app\common\exception\MiException;
use app\model\site\SiteAddress;
use app\site\controller\AController;
use app\site\controller\traits\CurdTrait;
use app\site\service\site\SiteAddressService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Request;


class SiteAddressController extends AController
{

    /**
     * @throws DbException
     */
    public function index(){
        return $this->reqSer->setService(SiteAddressService::instance())->setPaginate()->getResponse();
    }

    /**
     * @throws MiException
     */
    public function add(){
        return $this->reqSer->setService(SiteAddressService::instance())->add()->getResponse();
    }

    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws MiException
     */
    public function edit(\app\Request $request){
        if($request->isPost()){
            return $this->reqSer->setService(SiteAddressService::instance())->edit()->getResponse();
        }else{
            return $this->reqSer->setService(SiteAddressService::instance())->setFind()->getResponse();
        }
    }

    /**
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws MiException
     */
    public function del(){
        return $this->reqSer->setService(SiteAddressService::instance())->delete()->getResponse();
    }

    public function list(Request $request): \think\response\Json
    {
        $isTake = $request->param('is_take', '');
        $isRefund = $request->param('is_refund', '');
        $where = [];
        if ($isTake !==''){
            $where[] = ['is_take', '=', $isTake];
        }
        if ($isRefund !==''){
            $where[] = ['is_refund', '=', $isRefund];
        }
        $list = SiteAddress::where($where)->select();
        return jsonSuccess([
            'list' => $list,
        ]);
    }
}
